/**
 * [一句话描述该类的功能]
 *
 * @author : [kai]
 * @version : [v1.0]
 * @createTime : [2022/10/25 13:58]
 */


package com.find.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.find.controller.utils.R;
import com.find.domain.Comment;
import com.find.domain.Forum;
import com.find.service.CommentService;
import com.find.service.lmpl.CommentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags="评论相关功能")
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @ApiOperation(value = "根据id获取评论")
    @GetMapping("/{forum_id}")
    public R getComments(@PathVariable("forum_id")int forumId, HttpServletResponse response){
        QueryWrapper<Comment> qw=new QueryWrapper<Comment>();
        qw.eq("forum_id",forumId);
        qw.orderByDesc("comment_time");
        List<Comment> commentList=commentService.list(qw);
        response.setStatus(200);
        return new R(true,commentList);
    }
    @ApiOperation("插入评论")
    @PutMapping
    public  R insertComment(@RequestBody Comment comment,HttpServletResponse response){
        boolean flag=commentService.save(comment);
        response.setStatus(flag?200:500);
        return new R(flag,flag?"评论成功":"评论失败");
    }
    @ApiOperation("根据id删除评论")
    @DeleteMapping("/{comment_id}")
    public R deleteComment(@PathVariable("comment_id")int commentId,HttpServletResponse response){
        QueryWrapper<Comment> qw=new QueryWrapper<Comment>();
        qw.eq("comment_id",commentId);
        boolean flag=commentService.remove(qw);
        response.setStatus(flag?200:500);
        return new R(flag,flag?"删除成功":"删除失败");
    }
}
